package org.app.main;

import java.util.Map;

import org.app.domain.User;
import org.app.service.IContactService;
import org.app.service.IUserService;
import org.app.validator.UserValidator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class UserController {
	private static final Logger logger = LoggerFactory
			.getLogger(UserController.class);

	private IUserService userService;
	private UserValidator userValidator;
	
	@Autowired
	public UserController(IUserService userService, UserValidator userValidator) {
		this.userService = userService;
		this.userValidator = userValidator;
	}

	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "/user/add", method = RequestMethod.GET)
	public String show(@ModelAttribute("user") User user) {
		logger.info("add contact");
		user.setGender("M");
		user.setCountry("3");
		user.setName("asasas");
		return "user/add";
	}

	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "/user/add", method = RequestMethod.POST)
	public String addUser(@ModelAttribute("user") User user,
			BindingResult result) {
		userValidator.validate(user, result);
		logger.info("add contact");
		logger.info("name" + user.getName());

		logger.info("community:" + user.getCommunity().toString());
		logger.info("gender:" + user.getGender());

		return "user/add";
	}

	/**
	 * hello
	 * 
	 * @return
	 */
	@RequestMapping(value = "/user/list", method = RequestMethod.GET)
    public String listContacts(Map<String, Object> map) {
        return "user/list";
    }
}
